package models

import (
	"gorm.io/gorm"
	"time"
)

// Permission 权限表
type Permission struct {
	ID        int64          `json:"id" gorm:"primarykey;comment:主键ID"`
	CreatedAt time.Time      `json:"created_at" gorm:"comment:创建时间"`
	UpdatedAt time.Time      `json:"updated_at" gorm:"comment:更新时间"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"`

	Name        string `json:"name" gorm:"not null;size:50;comment:权限名称"`
	Code        string `json:"code" gorm:"uniqueIndex;not null;size:100;comment:权限代码"`
	Type        string `json:"type" gorm:"not null;size:20;comment:权限类型 menu菜单 button按钮 api接口"`
	Path        string `json:"path" gorm:"size:255;comment:路径"`
	Method      string `json:"method" gorm:"size:10;comment:请求方法"`
	Description string `json:"description" gorm:"size:255;comment:权限描述"`
	ParentID    int64  `json:"parent_id" gorm:"default:0;comment:父级ID"`
	Sort        int    `json:"sort" gorm:"default:0;comment:排序"`
	Status      int    `json:"status" gorm:"default:1;comment:状态 1正常 2禁用"`

	// 关联
	Roles []Role `json:"roles" gorm:"many2many:sys_role_permissions;"`
}

// TableName 指定表名
func (Permission) TableName() string {
	return "sys_permissions"
}

// PermissionCreateRequest 创建权限请求结构
type PermissionCreateRequest struct {
	Name        string `json:"name" binding:"required" example:"用户管理"`
	Code        string `json:"code" binding:"required" example:"user:manage"`
	Type        string `json:"type" binding:"required" example:"menu"`
	Path        string `json:"path" example:"/user"`
	Method      string `json:"method" example:"GET"`
	Description string `json:"description" example:"用户管理权限"`
	ParentID    int64  `json:"parent_id" example:"0"`
	Sort        int    `json:"sort" example:"1"`
	Status      int    `json:"status" example:"1"`
}

// PermissionUpdateRequest 更新权限请求结构
type PermissionUpdateRequest struct {
	Name        string `json:"name" example:"用户管理"`
	Code        string `json:"code" example:"user:manage"`
	Type        string `json:"type" example:"menu"`
	Path        string `json:"path" example:"/user"`
	Method      string `json:"method" example:"GET"`
	Description string `json:"description" example:"用户管理权限"`
	ParentID    int64  `json:"parent_id" example:"0"`
	Sort        int    `json:"sort" example:"1"`
	Status      int    `json:"status" example:"1"`
}

// PermissionListRequest 权限列表请求结构
type PermissionListRequest struct {
	Page     int    `json:"page" form:"page" example:"1"`
	PageSize int    `json:"page_size" form:"page_size" example:"10"`
	Name     string `json:"name" form:"name" example:"用户"`
	Code     string `json:"code" form:"code" example:"user"`
	Type     string `json:"type" form:"type" example:"menu"`
	Status   *int   `json:"status" form:"status" example:"1"`
}

// PermissionResponse 权限响应结构
type PermissionResponse struct {
	ID          int64                `json:"id"`
	CreatedAt   time.Time            `json:"created_at"`
	UpdatedAt   time.Time            `json:"updated_at"`
	Name        string               `json:"name"`
	Code        string               `json:"code"`
	Type        string               `json:"type"`
	Path        string               `json:"path"`
	Method      string               `json:"method"`
	Description string               `json:"description"`
	ParentID    int64                `json:"parent_id"`
	Sort        int                  `json:"sort"`
	Status      int                  `json:"status"`
	Children    []PermissionResponse `json:"children,omitempty"`
}

// PermissionListResponse 权限列表响应结构
type PermissionListResponse struct {
	List  []PermissionResponse `json:"list"`
	Total int64                `json:"total"`
	Page  int                  `json:"page"`
	Size  int                  `json:"size"`
}
